package com.bridgeintelligent.tag.engine.mvc.test;

import com.bridgeintelligent.tag.engine.api.DimInfo;
import com.bridgeintelligent.tag.engine.api.DimItemInfo;
import com.bridgeintelligent.tag.engine.core.cache.CacheConfig;
import com.bridgeintelligent.tag.engine.core.cache.CacheUtils;
import com.bridgeintelligent.tag.engine.es.base.ESQueryUtils;
import com.bridgeintelligent.tag.mapper.DimDataMapper;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created by JackCheung on 2019/10/10 4:00 PM.
 */
@AllArgsConstructor
@Service
public class EngineDataTestService {

    private DimDataMapper dimDataMapper;

    /**
     *      * 造测试数据
     *
     * @param count
     * @param indexName
     */
    public void dataHandler(Integer count, String indexName) {
        List<String> list = new ArrayList<>(1000);
        for (int i = 0; i < count; i++) {
            if (i < 10) {
                list.add("custNo_000" + i);
            }
            if (10 < i && i < 100) {
                list.add("custNo_00" + i);
            } else {
                list.add("custNo_0" + i);
            }
        }
//        List<DimItemInfo> itemInfoList = dimDataMapper.findDimItemList();
        Map<String, DimInfo> dimInfoMap = CacheUtils.getCacheMap(CacheConfig.DIM_INFO);
        Map<String, DimItemInfo> dimItemInfoMap = CacheUtils.getCacheMap(CacheConfig.DIM_ITEM_INFO);
        ESQueryUtils.updateIndexMapping(indexName);
//        String indexName, Map<String, Map<String, Object>> map
        Map<String, Map<String, Object>> map = new HashMap<>();
        Random random = new Random();
        for (int i = 0; i < count; i++) {
            Map<String, Object> sourceMap = new HashMap<>();
            sourceMap.put("cust_id", list.get(i));
            if (i < 100) {
                sourceMap.put("001", "0");
                sourceMap.put("003", random.nextInt(100));
                sourceMap.put("004", "0");
                sourceMap.put("005", "0");
            } else if (100 < i && i < 200) {
                sourceMap.put("001", "1");
                sourceMap.put("002", "清华大学");
                sourceMap.put("003", random.nextInt(100) + 100);
                sourceMap.put("004", "0");
                sourceMap.put("005", "1");
            } else if (200 < i && i < 300) {
                sourceMap.put("001", "0");
                sourceMap.put("002", "北京大学");
                sourceMap.put("003", random.nextInt(100) + 200);
                sourceMap.put("005", "2");
            } else if (300 < i && i < 400) {
                sourceMap.put("001", "1");
                sourceMap.put("002", "深圳大学");
                sourceMap.put("003", random.nextInt(100) + 300);
                sourceMap.put("005", "2");
            } else if (400 < i && i < 650) {
                sourceMap.put("001", "1");
                sourceMap.put("002", "上海大学");
                sourceMap.put("003", random.nextInt(100) + 400);
                sourceMap.put("005", "3");
            } else if (650 < i && i < 1000) {
                sourceMap.put("001", "0");
                sourceMap.put("002", "浙江大学");
                sourceMap.put("003", random.nextInt(100) + 500);
                sourceMap.put("005", "4");
            }
            map.put(list.get(i), sourceMap);
        }
        ESQueryUtils.bulkInsert(indexName, map);
    }
}
